package com.app.babygo;

import java.util.ArrayList;

import com.app.babygo.dbservice.AchievementAdapter;
import com.app.babygo.dbservice.ChildAdapter;
import com.app.babygo.exceptions.InternalDBException;
import com.app.babygo.exceptions.ObjectNotFoundException;
import com.app.babygo.help.MessageHelper;
import com.app.babygo.obj.AchievementTask;
import com.app.babygo.obj.Child;
import com.app.babygo.obj.ChildAchievement;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Testpage extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_testpage);
		Button testBtn = (Button) this.findViewById(R.id.button_ac);
		testBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//testChild();	
				testAchievement();
			};
		});
	}
	
	public void testAchievement() {
		Child child = new Child("test name","pid", 1, 2, "test info", "");
		ChildAdapter childAdapter = new ChildAdapter(this);
		AchievementAdapter aa = new AchievementAdapter(this);		
		try {
			childAdapter.open();
			aa.open();
			childAdapter.addChild(child);
			aa.addAchievementTask(new AchievementTask("task1", "t1"));
			aa.addAchievementTask(new AchievementTask("task2", "t2"));
			aa.addAchievementTask(new AchievementTask("task3", "t3"));
			ArrayList<AchievementTask> tasks = aa.getAchievementTaskList();
			if (tasks.size() != 3){
				MessageHelper.showMsg(this, "Test Result", "Test 1 Failed! (getList or add)");
			}
			for (AchievementTask task : tasks){
				Log.d("Manual Check", task.toString());
				if (task.getTaskname().equalsIgnoreCase("task2"))
				{
					// skip task2
					continue;
				}
				task.setTaskdescription("updated");
				aa.updateAchievementTask(task);
				AchievementTask testGetSingle = aa.getAchievementTask(task.getTaskid());
				Log.d("Manual Check", testGetSingle.toString());
			}
			
			ArrayList<AchievementTask> tasks2 = aa.getAchievementTaskList();
			if (tasks2.size() != 3){
				MessageHelper.showMsg(this, "Test Result", "Test 2 Failed! (update or getList)");
			}
			for (AchievementTask task : tasks2){
				if (task.getTaskname().equalsIgnoreCase("task2"))
				{
					// skip task2
					continue;
				}
				if (!aa.unlockAchievement(task.getTaskid(), child.getCid(), "/test/path"))
				{
					MessageHelper.showMsg(this, "Test Result", "Test 3 Failed! (unlock achievement)");
				}
			}
			ArrayList<ChildAchievement> ca = aa.getChildAchievementList(child.getCid());
			if (ca.size() != 2){
				MessageHelper.showMsg(this, "Test Result", "Test 4 Failed! (add or getList)");
			}
			
			ChildAchievement t = ca.get(0);
			t.setPicturePath("updated");
			aa.updateChildAchievement(t);
			ArrayList<ChildAchievement> ca2 = aa.getChildAchievementList(child.getCid());
			if (ca2.size() != 2){
				MessageHelper.showMsg(this, "Test Result", "Test 5 Failed! (update or getList)");
			}
			for (ChildAchievement t2: ca2){
				Log.d("Manual Check", t2.toString());
			}
			for (AchievementTask task : tasks2){
				aa.deleteAchievementTask(task.getTaskid());
			}
			tasks2 = aa.getAchievementTaskList();
			if (tasks2.size() != 0){
				MessageHelper.showMsg(this, "Test Result", "Test 6 Failed! (delete)");
			}
			ArrayList<ChildAchievement> ca3 = aa.getChildAchievementList(child.getCid());
			if (ca3.size() != 0){
				MessageHelper.showMsg(this, "Test Result", "Test 7 Failed! (Cascade)");
			}
			if (!childAdapter.deleteChild(child.getCid())) {
				MessageHelper.showMsg(this, "Test Result", "Test 8 Failed! (delete child)");
			}
			MessageHelper.showMsg(this, "Test Result", "Pass");
			
		} catch (InternalDBException e) {
			MessageHelper.showMsg(this, "Error", e.getMessage());
			Log.e("TestError", e.getMessage());
		} catch (ObjectNotFoundException e) {
			Log.e("TestError", e.getMessage());
		}
		finally
		{
			childAdapter.close();
		}
	}
	
	public void testChild(){
		Child child = new Child("test name","pid", 1, 2, "test info", "");
		ChildAdapter adapter = new ChildAdapter(this);
		try {
			adapter.open();
			adapter.addChild(child);
			Child child2 = adapter.getChildInfo(child.getCid());
			if (!child.toString().equalsIgnoreCase(child2.toString())) {
				MessageHelper.showMsg(this, "Test Result", "Test 1 Failed! (add or get)");
			}
			Log.d("Manual Check", child2.toString());
			child.setChildinfo("Updated info");
			child.setChildgender(500);
			adapter.updateChildInfo(child);
			child2 = adapter.getChildInfo(child.getCid());
			if (!child.toString().equalsIgnoreCase(child2.toString())) {
				MessageHelper.showMsg(this, "Test Result", "Test 2 Failed! (get or update)");
			}
			Log.d("Manual Check", child2.toString());
			Child child3 = new Child("test name","pid", 1, 2, "test info", "");
			adapter.addChild(child3);
			ArrayList<Child> list = adapter.getChildList(child2.getChildparentid());
			if (list.size() != 2){
				MessageHelper.showMsg(this, "Test Result", "Test 3 Failed! (get list)");
			}
			for (Child i : list) {
				if (!adapter.deleteChild(i.getCid())) {
					MessageHelper.showMsg(this, "Test Result", "Test 4 Failed! (delete)");
				}
			}
			

			
			try
			{
				Log.d("Test", "Final test.");
				adapter.getChildInfo(child.getCid());
			}catch (ObjectNotFoundException e) {
				Log.d("Test", "Added Child.");
				MessageHelper.showMsg(this, "Test Result", "Pass");
			}
		} catch (InternalDBException e) {
			MessageHelper.showMsg(this, "Error", e.getMessage());
			Log.e("TestError", e.getMessage());
		} catch (ObjectNotFoundException e) {
			Log.e("TestError", e.getMessage());
		}
		finally
		{
			adapter.close();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.testpage, menu);
		return true;
	}

}
